IE582 - Fall2021
HOMEWORK 1
TASK 1
D=(1:15)
set.seed(582)
dim1=array(runif(1000*D[1],-1,1), dim=c(1000, D[1]))
dim2=array(runif(1000*D[2],-1,1), dim=c(1000,D[2]))
dim3=array(runif(1000*D[3],-1,1),dim=c(1000,D[3]))
dim4=array(runif(1000*D[4],-1,1), dim=c(1000,D[4]))
dim5=array(runif(1000*D[5],-1,1), dim=c(1000,D[5]))
dim6=array(runif(1000*D[6],-1,1), dim=c(1000,D[6]))
dim7=array(runif(1000*D[7],-1,1), dim=c(1000,D[7]))
dim8=array(runif(1000*D[8],-1,1), dim=c(1000,D[8]))
dim9=array(runif(1000*D[9],-1,1), dim=c(1000,D[9]))
dim10=array(runif(1000*D[10],-1,1), dim=c(1000,D[10]))
dim11=array(runif(1000*D[11],-1,1), dim=c(1000,D[11]))
dim12=array(runif(1000*D[12],-1,1), dim=c(1000,D[12]))
dim13=array(runif(1000*D[13],-1,1), dim=c(1000,D[13]))
dim14=array(runif(1000*D[14],-1,1), dim=c(1000,D[14]))
dim15=array(runif(1000*D[15],-1,1), dim=c(1000,D[15]))
A)
distance1=array()
for (i in 1:nrow(dim1)){
sum=0
for(j in 1:ncol(dim1)){
sum = sum + (dim1[i,j]**2)
}
distance1[i] = sqrt(sum)
}
distance2=array()
for (i in 1:nrow(dim2)){
sum=0
for(j in 1:ncol(dim2)){
sum = sum + (dim2[i,j]**2)
}
distance2[i] = sqrt(sum)
}
distance3=array()
for (i in 1:nrow(dim3)){
sum=0
for(j in 1:ncol(dim3)){
sum = sum + (dim3[i,j]**2)
}
distance3[i] = sqrt(sum)
}
distance4=array()
for (i in 1:nrow(dim4)){
sum=0
for(j in 1:ncol(dim4)){
sum = sum + (dim4[i,j]**2)
}
distance4[i] = sqrt(sum)
}
distance5=array()
for (i in 1:nrow(dim5)){
sum=0
for(j in 1:ncol(dim5)){
sum = sum + (dim5[i,j]**2)
}
distance5[i] = sqrt(sum)
}
distance6=array()
for (i in 1:nrow(dim6)){
sum=0
for(j in 1:ncol(dim6)){
sum = sum + (dim6[i,j]**2)
}
distance6[i] = sqrt(sum)
}
distance7=array()
for (i in 1:nrow(dim7)){
sum=0
for(j in 1:ncol(dim7)){
sum = sum + (dim7[i,j]**2)
}
distance7[i] = sqrt(sum)
}
distance8=array()
for (i in 1:nrow(dim8)){
sum=0
for(j in 1:ncol(dim8)){
sum = sum + (dim8[i,j]**2)
}
distance8[i] = sqrt(sum)
}
distance9=array()
for (i in 1:nrow(dim9)){
sum=0
for(j in 1:ncol(dim9)){
sum = sum + (dim9[i,j]**2)
}
distance9[i] = sqrt(sum)
}
distance10=array()
for (i in 1:nrow(dim10)){
sum=0
for(j in 1:ncol(dim10)){
sum = sum + (dim10[i,j]**2)
}
distance10[i] = sqrt(sum)
}
distance11=array()
for (i in 1:nrow(dim11)){
sum=0
for(j in 1:ncol(dim11)){
sum = sum + (dim11[i,j]**2)
}
distance11[i] = sqrt(sum)
}
distance12=array()
for (i in 1:nrow(dim12)){
sum=0
for(j in 1:ncol(dim12)){
sum = sum + (dim12[i,j]**2)
}
distance12[i] = sqrt(sum)
}
distance13=array()
for (i in 1:nrow(dim13)){
sum=0
for(j in 1:ncol(dim13)){
sum = sum + (dim13[i,j]**2)
}
distance13[i] = sqrt(sum)
}
distance14=array()
for (i in 1:nrow(dim14)){
sum=0
for(j in 1:ncol(dim14)){
sum = sum + (dim14[i,j]**2)
}
distance14[i] = sqrt(sum)
}
distance15=array()
for (i in 1:nrow(dim15)){
sum=0
for(j in 1:ncol(dim15)){
sum = sum + (dim15[i,j]**2)
}
distance15[i] = sqrt(sum)
}
distances = matrix(c(distance1,distance2,distance3,
distance4,distance5,distance6,distance7,
distance8,distance9,distance10,distance11,
distance12,distance13,distance14,distance15), nrow=1000,ncol=15 )
percentages=array()
for(i in 1:ncol(distances)){
percentages[i]= sum(distances[,i]<1)/1000
}
Fractions of points within distance 1
percentages
plot(y=percentages, x=c(1:15), main="Fraction of Points Within Distance 1",xlab = "Dimension",
ylab="Fraction")
B)
For D=2 The fraction approximates the ratio of the volume of the unit circle to the square inscribing it.
The area of the circle is calculated as:
(Π*r^2)
The area of the square inscribing the circle with r=1 is 4. So;
0.799 = (Π*r^2) / 4 where r=1
Π is approximately calculated as:
pi_approx_dim2 = 0.799*4
pi_approx_dim2
For D=3 The fraction approximates the ratio of the volume of the unit sphere to the cube inscribing it.
The volume of the circle is calculated as:
(4/3)Πr^3
The volume of the cube inscribing the sphere with r=1 is 8. So;
0.524 = (4/3)Πr^3 / 8 where r=1
Π is approximately calculated as:
pi_approx_dim3 = 0.524*6
pi_approx_dim3
C)
To observe the approximation of Π I will perform the calculations for larger sample sizes for 2 dimensional plane and 3 dimensional space.
For D=2
set.seed(58)
dim2_5000=array(runif(5000*D[2],-1,1), dim=c(5000,D[2]))
dim2_10000=array(runif(10000*D[2],-1,1), dim=c(10000,D[2]))
dim2_25000=array(runif(25000*D[2],-1,1), dim=c(25000,D[2]))
dim2_100000=array(runif(100000*D[2],-1,1), dim=c(100000,D[2]))
dim2_50000=array(runif(50000*D[2],-1,1), dim=c(50000,D[2]))
distance2_5000=array()
for (i in 1:nrow(dim2_5000)){
sum=0
for(j in 1:ncol(dim2_5000)){
sum = sum + (dim2_5000[i,j]**2)
}
distance2_5000[i] = sqrt(sum)
}
percentages_2d=array()
percentages_2d[1] = percentages[2]
percentages_2d[2]= sum(distance2_5000<1)/5000
distance2_10000=array()
for (i in 1:nrow(dim2_10000)){
sum=0
for(j in 1:ncol(dim2_10000)){
sum = sum + (dim2_10000[i,j]**2)
}
distance2_10000[i] = sqrt(sum)
}
percentages_2d[3]= sum(distance2_10000<1)/10000
distance2_25000=array()
for (i in 1:nrow(dim2_25000)){
sum=0
for(j in 1:ncol(dim2_25000)){
sum = sum + (dim2_25000[i,j]**2)
}
distance2_25000[i] = sqrt(sum)
}
percentages_2d[4]= sum(distance2_25000<1)/25000
distance2_50000=array()
for (i in 1:nrow(dim2_50000)){
sum=0
for(j in 1:ncol(dim2_50000)){
sum = sum + (dim2_50000[i,j]**2)
}
distance2_50000[i] = sqrt(sum)
}
percentages_2d[5]= sum(distance2_50000<1)/50000
distance2_100000=array()
for (i in 1:nrow(dim2_100000)){
sum=0
for(j in 1:ncol(dim2_100000)){
sum = sum + (dim2_100000[i,j]**2)
}
distance2_100000[i] = sqrt(sum)
}
percentages_2d[6]= sum(distance2_100000<1)/100000
pi_approx_2d = percentages_2d*4
plot(y=pi_approx_2d, x=c(1,5,10,25,50,100),main="Approximation of pi for D=2",xlab = "Sample Size(*1000)",
ylab="Fraction")
For D=3
set.seed(58)
dim3_5000=array(runif(5000*D[3],-1,1), dim=c(5000,D[3]))
dim3_10000=array(runif(10000*D[3],-1,1), dim=c(10000,D[3]))
dim3_25000=array(runif(25000*D[3],-1,1), dim=c(25000,D[3]))
dim3_100000=array(runif(100000*D[3],-1,1), dim=c(100000,D[3]))
dim3_50000=array(runif(50000*D[3],-1,1), dim=c(50000,D[3]))
distance3_5000=array()
for (i in 1:nrow(dim3_5000)){
sum=0
for(j in 1:ncol(dim3_5000)){
sum = sum + (dim3_5000[i,j]**2)
}
distance3_5000[i] = sqrt(sum)
}
percentages_3d=array()
percentages_3d[1] = percentages[3]
percentages_3d[2]= sum(distance3_5000<1)/5000
distance3_10000=array()
for (i in 1:nrow(dim3_10000)){
sum=0
for(j in 1:ncol(dim3_10000)){
sum = sum + (dim3_10000[i,j]**2)
}
distance3_10000[i] = sqrt(sum)
}
percentages_3d[3]= sum(distance3_10000<1)/10000
distance3_25000=array()
for (i in 1:nrow(dim3_25000)){
sum=0
for(j in 1:ncol(dim3_25000)){
sum = sum + (dim3_25000[i,j]**2)
}
distance3_25000[i] = sqrt(sum)
}
percentages_3d[4]= sum(distance3_25000<1)/25000
distance3_50000=array()
for (i in 1:nrow(dim3_50000)){
sum=0
for(j in 1:ncol(dim3_50000)){
sum = sum + (dim3_50000[i,j]**2)
}
distance3_50000[i] = sqrt(sum)
}
percentages_3d[5]= sum(distance3_50000<1)/50000
distance3_100000=array()
for (i in 1:nrow(dim3_100000)){
sum=0
for(j in 1:ncol(dim3_100000)){
sum = sum + (dim3_100000[i,j]**2)
}
distance3_100000[i] = sqrt(sum)
}
percentages_3d[6]= sum(distance3_100000<1)/100000
pi_approx_3d = percentages_3d*6
plot(y=pi_approx_3d, x=c(1,5,10,25,50,100),main="Approximation of pi for D=3",xlab = "Sample Size(*1000)",
ylab="Fraction")
For both D=2 and D=3 as the sample size gets larger approximation of pi gets closer to the actual value. Fluctutations of the approximated values around the real value is expected as a result of randomness.
For D=3 the approximation is very close to the real value when the sample size is 1000.This can also be considered as a result of randomness. As the sample size gets larger approximation shows the expected behavior.
D) NEAREST NEIGHBORS
set.seed(52)
dim1_test=array(runif(100*D[1],-1,1), dim=c(100, D[1]))
dim2_test=array(runif(100*D[2],-1,1), dim=c(100,D[2]))
dim3_test=array(runif(100*D[3],-1,1),dim=c(100,D[3]))
dim4_test=array(runif(100*D[4],-1,1), dim=c(100,D[4]))
dim5_test=array(runif(100*D[5],-1,1), dim=c(100,D[5]))
dim6_test=array(runif(100*D[6],-1,1), dim=c(100,D[6]))
dim7_test=array(runif(100*D[7],-1,1), dim=c(100,D[7]))
dim8_test=array(runif(100*D[8],-1,1), dim=c(100,D[8]))
dim9_test=array(runif(100*D[9],-1,1), dim=c(100,D[9]))
dim10_test=array(runif(100*D[10],-1,1), dim=c(100,D[10]))
dim11_test=array(runif(100*D[11],-1,1), dim=c(100,D[11]))
dim12_test=array(runif(100*D[12],-1,1), dim=c(100,D[12]))
dim13_test=array(runif(100*D[13],-1,1), dim=c(100,D[13]))
dim14_test=array(runif(100*D[14],-1,1), dim=c(100,D[14]))
dim15_test=array(runif(100*D[15],-1,1), dim=c(100,D[15]))
neighbors1=matrix(,100,1000)
for(k in 1:nrow(dim1_test)){
for (i in 1:nrow(dim1)){
sum=0
for(j in 1:ncol(dim1)){
sum = sum + ((dim1_test[k,j]-dim1[i,j])**2)
}
neighbors1[k,i] = sqrt(sum)
}
}
nearest_neighbors1 = array()
for(i in 1:nrow(neighbors1)){
nearest_neighbors1[i]=min(neighbors1[i,])
}
neighbors2=matrix(,100,1000)
for(k in 1:nrow(dim2_test)){
for (i in 1:nrow(dim2)){
sum=0
for(j in 1:ncol(dim2)){
sum = sum + ((dim2_test[k,j]-dim2[i,j])**2)
}
neighbors2[k,i] = sqrt(sum)
}
}
nearest_neighbors2 = array()
for(i in 1:nrow(neighbors2)){
nearest_neighbors2[i]=min(neighbors2[i,])
}
neighbors3=matrix(,100,1000)
for(k in 1:nrow(dim3_test)){
for (i in 1:nrow(dim3)){
sum=0
for(j in 1:ncol(dim3)){
sum = sum + ((dim3_test[k,j]-dim3[i,j])**2)
}
neighbors3[k,i] = sqrt(sum)
}
}
nearest_neighbors3 = array()
for(i in 1:nrow(neighbors3)){
nearest_neighbors3[i]=min(neighbors3[i,])
}
neighbors4=matrix(,100,1000)
for(k in 1:nrow(dim4_test)){
for (i in 1:nrow(dim4)){
sum=0
for(j in 1:ncol(dim4)){
sum = sum + ((dim4_test[k,j]-dim4[i,j])**2)
}
neighbors4[k,i] = sqrt(sum)
}
}
nearest_neighbors4 = array()
for(i in 1:nrow(neighbors4)){
nearest_neighbors4[i]=min(neighbors4[i,])
}
neighbors5=matrix(,100,1000)
for(k in 1:nrow(dim5_test)){
for (i in 1:nrow(dim5)){
sum=0
for(j in 1:ncol(dim5)){
sum = sum + ((dim5_test[k,j]-dim5[i,j])**2)
}
neighbors5[k,i] = sqrt(sum)
}
}
nearest_neighbors5 = array()
for(i in 1:nrow(neighbors5)){
nearest_neighbors5[i]=min(neighbors5[i,])
}
neighbors6=matrix(,100,1000)
for(k in 1:nrow(dim6_test)){
for (i in 1:nrow(dim6)){
sum=0
for(j in 1:ncol(dim6)){
sum = sum + ((dim6_test[k,j]-dim6[i,j])**2)
}
neighbors6[k,i] = sqrt(sum)
}
}
nearest_neighbors6 = array()
for(i in 1:nrow(neighbors6)){
nearest_neighbors6[i]=min(neighbors6[i,])
}
neighbors7=matrix(,100,1000)
for(k in 1:nrow(dim7_test)){
for (i in 1:nrow(dim7)){
sum=0
for(j in 1:ncol(dim7)){
sum = sum + ((dim7_test[k,j]-dim7[i,j])**2)
}
neighbors7[k,i] = sqrt(sum)
}
}
nearest_neighbors7 = array()
for(i in 1:nrow(neighbors7)){
nearest_neighbors7[i]=min(neighbors7[i,])
}
neighbors8=matrix(,100,1000)
for(k in 1:nrow(dim8_test)){
for (i in 1:nrow(dim8)){
sum=0
for(j in 1:ncol(dim8)){
sum = sum + ((dim8_test[k,j]-dim8[i,j])**2)
}
neighbors8[k,i] = sqrt(sum)
}
}
nearest_neighbors8 = array()
for(i in 1:nrow(neighbors8)){
nearest_neighbors8[i]=min(neighbors8[i,])
}
neighbors9=matrix(,100,1000)
for(k in 1:nrow(dim9_test)){
for (i in 1:nrow(dim9)){
sum=0
for(j in 1:ncol(dim9)){
sum = sum + ((dim9_test[k,j]-dim9[i,j])**2)
}
neighbors9[k,i] = sqrt(sum)
}
}
nearest_neighbors9 = array()
for(i in 1:nrow(neighbors9)){
nearest_neighbors9[i]=min(neighbors9[i,])
}
neighbors10=matrix(,100,1000)
for(k in 1:nrow(dim10_test)){
for (i in 1:nrow(dim10)){
sum=0
for(j in 1:ncol(dim10)){
sum = sum + ((dim10_test[k,j]-dim10[i,j])**2)
}
neighbors10[k,i] = sqrt(sum)
}
}
nearest_neighbors10 = array()
for(i in 1:nrow(neighbors10)){
nearest_neighbors10[i]=min(neighbors10[i,])
}
neighbors11=matrix(,100,1000)
for(k in 1:nrow(dim11_test)){
for (i in 1:nrow(dim11)){
sum=0
for(j in 1:ncol(dim11)){
sum = sum + ((dim11_test[k,j]-dim11[i,j])**2)
}
neighbors11[k,i] = sqrt(sum)
}
}
nearest_neighbors11 = array()
for(i in 1:nrow(neighbors11)){
nearest_neighbors11[i]=min(neighbors11[i,])
}
neighbors12=matrix(,100,1000)
for(k in 1:nrow(dim12_test)){
for (i in 1:nrow(dim12)){
sum=0
for(j in 1:ncol(dim12)){
sum = sum + ((dim12_test[k,j]-dim12[i,j])**2)
}
neighbors12[k,i] = sqrt(sum)
}
}
nearest_neighbors12= array()
for(i in 1:nrow(neighbors12)){
nearest_neighbors12[i]=min(neighbors12[i,])
}
neighbors13=matrix(,100,1000)
for(k in 1:nrow(dim13_test)){
for (i in 1:nrow(dim13)){
sum=0
for(j in 1:ncol(dim13)){
sum = sum + ((dim13_test[k,j]-dim13[i,j])**2)
}
neighbors13[k,i] = sqrt(sum)
}
}
nearest_neighbors13 = array()
for(i in 1:nrow(neighbors13)){
nearest_neighbors13[i]=min(neighbors13[i,])
}
neighbors14=matrix(,100,1000)
for(k in 1:nrow(dim14_test)){
for (i in 1:nrow(dim14)){
sum=0
for(j in 1:ncol(dim14)){
sum = sum + ((dim14_test[k,j]-dim14[i,j])**2)
}
neighbors14[k,i] = sqrt(sum)
}
}
nearest_neighbors14 = array()
for(i in 1:nrow(neighbors14)){
nearest_neighbors14[i]=min(neighbors14[i,])
}
neighbors15=matrix(,100,1000)
for(k in 1:nrow(dim15_test)){
for (i in 1:nrow(dim15)){
sum=0
for(j in 1:ncol(dim15)){
sum = sum + ((dim15_test[k,j]-dim15[i,j])**2)
}
neighbors15[k,i] = sqrt(sum)
}
}
nearest_neighbors15 = array()
for(i in 1:nrow(neighbors15)){
nearest_neighbors15[i]=min(neighbors15[i,])
}
avg_distances = c(mean(nearest_neighbors1),mean(nearest_neighbors2),mean(nearest_neighbors3),
mean(nearest_neighbors4),mean(nearest_neighbors5),mean(nearest_neighbors6),mean(nearest_neighbors7),
mean(nearest_neighbors8),mean(nearest_neighbors9),mean(nearest_neighbors10),mean(nearest_neighbors11),
mean(nearest_neighbors12),mean(nearest_neighbors13),mean(nearest_neighbors14),mean(nearest_neighbors15))
plot(y=avg_distances, x=c(1:15),main="Average Nearest Neighbor Distances",xlab = "Dimension",
ylab="Distance")
TASK 2
require(jpeg)
setwd("C:/Users/Pınar YILDIRIM/Desktop")
Loading required package: jpeg
my_image=readJPEG("homework1.jpg")
The image is read with the readJPEG function and stored as my_image. As stated below the object is numeric array, which consists of "doubles", with dimensions 512x512x3. The results are consistent with the information given in the homework description. There are 3 512x512 matrices to represent the color intensity of each pixel for 3 colors RED, GREEN and BLUE.
is.numeric(my_image)
is.character(my_image)
typeof(my_image)
class(my_image)
dim(my_image)
str(my_image)
num [1:512, 1:512, 1:3] 0.796 0.792 0.796 0.804 0.808 ...
plot(c(100, 150), c(400, 450), type = "n", xlab = "", ylab = "")
rasterImage((my_image), 100, 400, 150, 450)
When matrices representing each channel is displayed the result is a black and white image that only demonstrates the instensity of the colors.
plot(c(100, 400), c(400, 450), type = "n", xlab = "", ylab = "")
rasterImage((my_image[,,1]), 100, 400, 200, 450)
rasterImage((my_image[,,2]), 200, 400, 300, 450)
rasterImage((my_image[,,3]), 300, 400, 400, 450)
To observe colors I created a matrix of zeros with size 512x512. Then I created 3 arrays with sizes 512x512x3. I assigned each color red, blue and green to the corresponding space of one matrix and filled remaining spaces with the zero matrices.
zeros=array(rep(0,(512*512)),dim=c(512,512))
red=array(c(my_image[,,1],zeros,zeros),dim=c(512,512,3))
green=array(c(zeros,my_image[,,2],zeros),dim=c(512,512,3))
blue=array(c(zeros,zeros,my_image[,,3]),dim=c(512,512,3))
plot(c(100, 150), c(400, 450), type = "n", xlab = "", ylab = "")
rasterImage((red), 100, 400, 150, 450)
plot(c(100, 150), c(400, 450), type = "n", xlab = "", ylab = "")
rasterImage((green), 100, 400, 150, 450)
plot(c(100, 150), c(400, 450), type = "n", xlab = "", ylab = "")
rasterImage((blue), 100, 400, 150, 450)
When I put the related elements of matrices red, blue and green together and discard the zeros I get the original image.
comp=array(c(red[,,1],green[,,2],blue[,,3]),dim=c(512,512,3))
plot(c(100, 150), c(400, 450), type = "n", xlab = "", ylab = "")
rasterImage((comp), 100, 400, 150, 450)
For each channel column means are calculated and plotted as below.
r = my_image[,,1]
g = my_image[,,2]
b = my_image[,,3]
r_colmeans =array()
for (i in 1:512){
r_colmeans[i] = mean(r[,i])
}
g_colmeans =array()
for (i in 1:512){
g_colmeans[i] = mean(g[,i])
}
b_colmeans =array()
for (i in 1:512){
b_colmeans[i] = mean(b[,i])
}
require(ggplot2)
(ggplot() + geom_line(aes(x=c(1:512), y=r_colmeans, color="red"))
+geom_line(aes(x=c(1:512), y=g_colmeans, color="green"))
+geom_line(aes(x=c(1:512), y=b_colmeans, color="blue"))
+ labs(title = "Column Means", x="Column", y="Mean")
)
Loading required package: ggplot2 Registered S3 methods overwritten by 'ggplot2': method from [.quosures rlang c.quosures rlang print.quosures rlang
I split the image into two halves and subtracted the right half from the left. The two halves and new image is shown below negative pixel values are set to zero:
r_left = r[,1:256]
r_right = r[,512:257]
r_new=r_left-r_right
for (i in 1:length(r_new)){
if(r_new[i]<0){
r_new[i] = 0
}
}
g_left = g[,1:256]
g_right = g[,512:257]
g_new=g_left-g_right
for (i in 1:length(g_new)){
if(g_new[i]<0){
g_new[i] = 0
}
}
b_left = b[,1:256]
b_right = b[,512:257]
b_new=b_left-b_right
for (i in 1:length(b_new)){
if(b_new[i]<0){
b_new[i] = 0
}
}
left =array(c(r_left,g_left,b_left),dim=c(512,256,3))
right =array(c(r_right,g_right,b_right),dim=c(512,256,3))
comp_new=array(c(r_new,g_new,b_new),dim=c(512,256,3))
plot(c(100, 150), c(400, 450), type = "n", xlab = "", ylab = "")
rasterImage((left), 100, 400, 150, 450)
plot(c(100, 150), c(400, 450), type = "n", xlab = "", ylab = "")
rasterImage((right), 100, 400, 150, 450)
plot(c(100, 150), c(400, 450), type = "n", xlab = "", ylab = "")
rasterImage((comp_new), 100, 400, 150, 450)
plot(c(100, 150), c(400, 450), type = "n", xlab = "", ylab = "")
rasterImage((b_left), 100, 400, 150, 450)
plot(c(100, 150), c(400, 450), type = "n", xlab = "", ylab = "")
rasterImage((b_right), 100, 400, 150, 450)
plot(c(100, 150), c(400, 450), type = "n", xlab = "", ylab = "")
rasterImage((b_new), 100, 400, 150, 450)
Red channel of new image is displayed below:
plot(c(100, 150), c(400, 450), type = "n", xlab = "", ylab = "")
rasterImage((r_left), 100, 400, 150, 450)
plot(c(100, 150), c(400, 450), type = "n", xlab = "", ylab = "")
rasterImage((r_right), 100, 400, 150, 450)
plot(c(100, 150), c(400, 450), type = "n", xlab = "", ylab = "")
rasterImage((r_new), 100, 400, 150, 450)
zeros_new=array(rep(0,(512*256)),dim=c(512,256))
red_new=array(c(r_new,zeros_new,zeros_new),dim=c(512,256,3))
plot(c(100, 150), c(400, 450), type = "n", xlab = "", ylab = "")
rasterImage((red_new), 100, 400, 150, 450)
Green channel of new image is displayed below:
plot(c(100, 150), c(400, 450), type = "n", xlab = "", ylab = "")
rasterImage((g_left), 100, 400, 150, 450)
plot(c(100, 150), c(400, 450), type = "n", xlab = "", ylab = "")
rasterImage((g_right), 100, 400, 150, 450)
plot(c(100, 150), c(400, 450), type = "n", xlab = "", ylab = "")
rasterImage((g_new), 100, 400, 150, 450)
green_new=array(c(zeros_new,g_new,zeros_new),dim=c(512,256,3))
plot(c(100, 150), c(400, 450), type = "n", xlab = "", ylab = "")
rasterImage((green_new), 100, 400, 150, 450)
Blue channel of new image is displayed below:
plot(c(100, 150), c(400, 450), type = "n", xlab = "", ylab = "")
rasterImage((b_left), 100, 400, 150, 450)
plot(c(100, 150), c(400, 450), type = "n", xlab = "", ylab = "")
rasterImage((b_right), 100, 400, 150, 450)
plot(c(100, 150), c(400, 450), type = "n", xlab = "", ylab = "")
rasterImage((b_new), 100, 400, 150, 450)
blue_new=array(c(zeros_new,zeros_new,b_new),dim=c(512,256,3))
plot(c(100, 150), c(400, 450), type = "n", xlab = "", ylab = "")
rasterImage((blue_new), 100, 400, 150, 450)
To create a noisy image noise is added to the each pixel of the original image. Noise values are generated from uniform distribution with minimum value of 0 and a maximum value of “0.1 * maximum pixel value observed” to each pixel value for each channel of original image.
Maximum pixel value observed is 1 and there are 47 pixels with value 1.
max(my_image)
sum(my_image==1)
my_image_noisy= array(,dim=c(512,512,3))
set.seed(23)
for(i in 1:3){
for(j in 1:512){
for(k in 1:512){
my_image_noisy[k,j,i] = my_image[k,j,i]+runif(1,0,0.1)
}
}
}
for(i in 1:3){
for(j in 1:512){
for(k in 1:512){
if(my_image_noisy[k,j,i] > 1){
my_image_noisy[k,j,i] = 1
}
}
}
}
plot(c(100, 150), c(400, 450), type = "n", xlab = "", ylab = "")
rasterImage((my_image_noisy), 100, 400, 150, 450)
plot(c(100, 400), c(400, 450), type = "n", xlab = "", ylab = "")
rasterImage((my_image_noisy[,,1]), 100, 400, 200, 450)
rasterImage((my_image_noisy[,,2]), 200, 400, 300, 450)
rasterImage((my_image_noisy[,,3]), 300, 400, 400, 450)
zeros=array(rep(0,(512*512)),dim=c(512,512))
red_noisy=array(c(my_image_noisy[,,1],zeros,zeros),dim=c(512,512,3))
green_noisy=array(c(zeros,my_image_noisy[,,2],zeros),dim=c(512,512,3))
blue_noisy=array(c(zeros,zeros,my_image_noisy[,,3]),dim=c(512,512,3))
plot(c(100, 150), c(400, 450), type = "n", xlab = "", ylab = "")
rasterImage((red_noisy), 100, 400, 150, 450)
plot(c(100, 150), c(400, 450), type = "n", xlab = "", ylab = "")
rasterImage((green_noisy), 100, 400, 150, 450)
plot(c(100, 150), c(400, 450), type = "n", xlab = "", ylab = "")
rasterImage((blue_noisy), 100, 400, 150, 450)